/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui.accordion;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;

import com.pearreview.ui.FormLabel;
import com.pearreview.ui.SmallPearPanel;
import com.pearreview.ui.slidebox.SlideOpenButton;

public class AccordionPanel extends SmallPearPanel {

	private static final long serialVersionUID = 4922313019407556520L;
	
	private FormLabel caption;
	private SlideOpenButton button;
	
	public AccordionPanel(String name) {
		super();
		setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		setBorder(BorderFactory.createMatteBorder(
	  			1, 1, 1, 1, new Color(0x828790)));
		
		this.setPreferredSize(new Dimension(25, 25));
		
		caption = new FormLabel(name);
		
		button = new SlideOpenButton();
		
		GroupLayout layout = new GroupLayout(this);
		setLayout(layout);
		layout.setHorizontalGroup(layout.createSequentialGroup()
				.addGap(10)
				.addComponent(button)
				.addGap(10)
				.addComponent(caption));
		layout.setVerticalGroup(
				layout.createSequentialGroup()
				.addGap(4)
				.addGroup(layout.createParallelGroup()
				.addComponent(button)
				.addComponent(caption)));
	}
	
	public void setOpened(boolean open) {
		setOpened(open, false);
	}
	
	public void setOpened(boolean open, boolean last) {
		this.button.setOpenState(open);
		if (open || last) {
			setBorder(BorderFactory.createMatteBorder(
		  			1, 1, 1, 1, new Color(0x828790)));
		} else {
			setBorder(BorderFactory.createMatteBorder(
		  			1, 1, 0, 1, new Color(0x828790)));
		}
	}
	
	public void addActionListener(ActionListener al) {
		button.addActionListener(al);
	}
}
